﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;
using RentHouse.DTO;
using RentHouse.IService;
using RentHouse.Service;
using RentHouse.Service.Entitys;
using RentHouse.Common;

namespace RentHouse.Service
{
    public class AdminUsersService : IAdminUsersService
    {
        DBModel _dbContext;
        public AdminUsersService(DBModel dbContext)
        {
            this._dbContext = dbContext;
        }

        /// <summary>using System.Web.Helpers;
        /// 分写
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public List<AdminUserDTO> GetList()
        {

            return _dbContext.T_AdminUsers.Select(e => new AdminUserDTO
            {
                Id = e.Id,
                Name = e.Name,
                CityId = e.CityId == null ? 0 : (long)e.CityId,
                Email = e.Email,
                LoginErrorTimes = (int)e.LoginErrorTimes,
                PhoneNum = e.PhoneNum,
                CityName = e.T_Cities.Name
            }).ToList();

        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="adminUserDTO"></param>
        /// <returns></returns>
        public string Create(AdminUserDTO adminUserDTO)
        {
            //获取密码盐
            string salt = CommonHelper.CreateVerifyCode();

            T_AdminUsers t_AdminUsers = new T_AdminUsers()
            {
                //Id = (int)adminUserDTO.Id,
                CityId = 1,
                Name = adminUserDTO.Name ?? "1",
                PhoneNum = adminUserDTO.PhoneNum ?? "1",
                Email = adminUserDTO.Email ?? "1",
                //LoginErrorTimes = adminUserDTO.LoginErrorTimes,
                LoginErrorTimes = 1,
                PasswordHash = CommonHelper.CalcMD5(adminUserDTO.Password + salt),
                PasswordSalt = salt,
                LastLoginErrorDateTime = DateTime.Now,
                CreateDateTime = DateTime.Now,
                IsDelete = true
            };
            _dbContext.T_AdminUsers.AddOrUpdate(t_AdminUsers);
            int i = _dbContext.SaveChanges();

            if (i <= 0)
            {
                return "error";
            }
            else
            {
                return "succeed";
            }

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="adminUserDTO"></param>
        /// <returns></returns>
        public string Delete(AdminUserDTO adminUserDTO)
        {
            int Id = (int)adminUserDTO.Id;

            List<T_HouseAppointments> list = _dbContext.T_HouseAppointments.Where(t => t.FollowAdminUserId == Id).ToList();
            if (list.Count > 0)
            {
                int i = 0;
                foreach (var item in list)
                {
                    T_HouseAppointments t_House = _dbContext.T_HouseAppointments.Where(t => t.Id == item.Id).FirstOrDefault();
                    _dbContext.Entry(t_House).State = EntityState.Deleted;
                    _dbContext.T_HouseAppointments.Remove(t_House);
                    i = i + _dbContext.SaveChanges();
                }
                if (i <= 0)
                {
                    return "error";
                }
                else if (i == list.Count)
                {
                    T_AdminUsers t_Admin = _dbContext.T_AdminUsers.Where(t => t.Id == Id).FirstOrDefault();
                    _dbContext.Entry(t_Admin).State = EntityState.Deleted;
                    _dbContext.T_AdminUsers.Remove(t_Admin);
                    i = _dbContext.SaveChanges();
                    if (i > 0)
                    {
                        return "succeed";
                    }
                    return "error";

                }

                return "error";
            }
            else if (Id != 0)
            {
                int i;
                T_AdminUsers t_Admin = _dbContext.T_AdminUsers.Where(t => t.Id == Id).FirstOrDefault();
                _dbContext.Entry(t_Admin).State = EntityState.Deleted;
                _dbContext.T_AdminUsers.Remove(t_Admin);
                i = _dbContext.SaveChanges();
                if (i > 0)
                {
                    return "succeed";
                }
                return "error";
            }
            else
            {

                return "error";

            }


        }
        /// <summary>
        /// 用户修改
        /// </summary>
        /// <param name="adminUserDTO"></param>
        /// <returns></returns>
        public string Update(AdminUserDTO adminUserDTO)
        {

            T_AdminUsers t_Admin = _dbContext.T_AdminUsers.Where(t => t.Id == adminUserDTO.Id).FirstOrDefault();

            t_Admin.Name = adminUserDTO.Name ?? t_Admin.Name;
            t_Admin.PhoneNum = adminUserDTO.PhoneNum ?? t_Admin.PhoneNum;
            t_Admin.Email = adminUserDTO.Email ?? t_Admin.Email;

            _dbContext.T_AdminUsers.AddOrUpdate(t_Admin);
            int i = _dbContext.SaveChanges();

            if (i <= 0)
            {
                return "error";
            }
            else
            {
                return "succeed";
            }
        }

        /// <summary>
        /// 用户名密码
        /// </summary>
        /// <param name="adminUserDTO"></param>
        /// <returns></returns>
        public string Login(AdminUserDTO adminUserDTO)
        {

            T_AdminUsers t_Admin = _dbContext.T_AdminUsers.Where(t => t.Name == adminUserDTO.Name).FirstOrDefault();

            if (t_Admin!=null)
            {
                string pwd = CommonHelper.CalcMD5(adminUserDTO.Password + t_Admin.PasswordSalt);

                if (t_Admin.PasswordHash == pwd)
                {
                    return "succeed";
                }
                else
                {
                    return "error";
                }
            }
            else
            {
                return "error";
            }


        }


    }
}
